package com.hc.server.actions;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Config;
import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

public class QUERY_HOTEL implements IAction {

	private static final String HID = "HID";
	private final static String CHECKIN_DATE = "CHECKIN_DATE";
	private final static String CHECKOUT_DATE = "CHECKOUT_DATE";
	
	private final static String NAME = "NAME";
	private static final String SN = "SN";
	private final static String ADD = "ADD";
	private final static String PIC = "PIC";
	private final static String USAGE = "USAGE";
	private final static String DES = "DES";
	private final static String	ROOMS = "ROOMS";
	private final static String ROOM = "ROOM";
	private final static String MPRICE = "MPRICE";	
	private final static String SPRICE = "SPRICE";
	private final static String STOCK = "STOCK";

	// MALL字段
	private final static String MALL_ID = "id";
	private final static String MALL_SN = "pdtSn";
	private final static String MALL_NAME = "name";
	private final static String MALL_ADDRESS = "address";
	// private final static String MALL_BRIEF = "brief";
	private final static String MALL_PICURI = "picUri";
	// private final static String MALL_PHONE = "phone";
	private final static String MALL_LV = "level";
	private final static String MALL_DES = "description";
	private final static String MALL_USAGE = "useExplain";
	//private final static String MALL_FEE = "feeExplain";
	//private final static String MALL_NOTES = "bookExplain";
	// private final static String MALL_PROMPT = "pdtExplain";
	private static final String MAll_MARKETPRICE = "marketPrice";
	private static final String MALL_SALEPRICE = "localPrice";
	private static final String MALL_STOCK = "stockCount";
	private final static String MALL_ROOMS = "rooms";
	
	private static final String MALL_ROOMTYPE =	"roomType";
	//		picUri
	private static final String MALL_BREAKFAST = "breakfast";
	

	@Override
	public void execute(Request req, Response resp) {

		// 1.处理参数
		int hotelID;
		String checkInDate;
		String checkOutDate;
		try {
			hotelID = req.getIntArg(HID);
			checkInDate = req.getStrArg(CHECKIN_DATE);
			checkOutDate = req.getStrArg(CHECKOUT_DATE);
		} catch (Exception e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD,
					Error.INVALID_ARGS_ERR_MSG);
			return;
		}

		// 2.查询
		MallResponse mResp = null;
		try {
			mResp = MallServerHelper.queryHotel(hotelID, checkInDate, checkOutDate);
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		} catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		}
		if (0 != mResp.getRetCode()) {
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}

		// 3.组织返回数据
		try {
			JSONObject mHotel = mResp.getResult();
			String id = mHotel.getString(MALL_ID);
			resp.appendRespContent(HID, id);
			String tName = mHotel.getString(MALL_NAME);
			resp.appendRespContent(NAME, tName);
			String tAddress = mHotel.getString(MALL_ADDRESS);
			resp.appendRespContent(ADD, tAddress);
			String tPicUrl = mHotel.getString(MALL_PICURI);
			resp.appendRespContent(PIC, tPicUrl);
			String lv = mHotel.getString(MALL_LV);
			resp.appendRespContent(MALL_LV, lv);
			// resp.appendRespContent(KEY_WORD, value);
			String tUsage = mHotel.getString(MALL_USAGE);
			resp.appendRespContent(USAGE, tUsage);
			String tDes = mHotel.getString(MALL_DES);
			resp.appendRespContent(DES, tDes);
			JSONArray tRooms = mHotel.getJSONArray(MALL_ROOMS);
			JSONArray rooms = new JSONArray();
			resp.appendRespContent(ROOMS, rooms);
			for(int i=0; i<tRooms.length(); i++){
				JSONObject troom = tRooms.getJSONObject(i);
				JSONObject room = new JSONObject();
				String sn = troom.getString(MALL_SN);
				room.put(SN, sn);
				int type = troom.getInt(MALL_ROOMTYPE);
				int breakfast = troom.getInt(MALL_BREAKFAST);
				String roomName = getRoomType(type, breakfast);
				room.put(ROOM, roomName);
				int mp = troom.getInt(MAll_MARKETPRICE);
				int sp = troom.getInt(MALL_SALEPRICE);
				room.put(MPRICE, mp);
				room.put(SPRICE, sp);
				int stock = troom.getInt(MALL_STOCK);
				room.put(STOCK, stock);
				rooms.put(room);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}
	}
	
	private String getRoomType(int type, int breakfast){
		String name = "";
		switch (type) {
			case Config.ROOM_TYPE_BJ:
				name += "标间";
				break;
			case Config.ROOM_TYPE_DCF:
				name += "大床房";
				break;
			case Config.ROOM_TYPE_TF:
				name += "套房";
				break;
		}
		switch (type) {
			case Config.ROOM_BREAKFAST_NONE:
				name += "(无早)";
				break;
			case Config.ROOM_BREAKFAST_ONE:
				name += "(单早)";
				break;
			case Config.ROOM_BREAKFAST_TOW:
				name += "(双早)";
				break;
		}
		return name;
	}

}
